<!DOCTYPE html>


<html lang="zh-Hans">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    大整数类设计文档 |  Veal&#39;s Blog
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

<link rel="alternate" href="/atom.xml" title="Veal's Blog" type="application/atom+xml">
<link href="https://cdn.bootcss.com/KaTeX/0.11.1/katex.min.css" rel="stylesheet" /></head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-bign"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  大整数类设计文档
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/08/14/bign/" class="article-date">
  <time datetime="2020-08-14T10:17:03.000Z" itemprop="datePublished">2020-08-14</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E7%AE%97%E6%B3%95%E7%AB%9E%E8%B5%9B/">算法竞赛</a> / <a class="article-category-link" href="/categories/%E7%AE%97%E6%B3%95%E7%AB%9E%E8%B5%9B/cpp%E5%9F%BA%E7%A1%80/">cpp基础</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">2.2k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">10 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p><em>大整数类断断续续写了两天，最后写除法的时候才发现最大的问题是没有明确的输入输出限制意识，导致隐藏bug</em><br>输入：必须为正整数，四则运算、比较运算符仅限于正整数之间<br>输出：减法输出可为负数，其余均应为非负整数。</p>
<p>完整代码见<a href="https://gitee.com/VealM/UVaOJ/blob/master/Chapter5/textbook/5-2-3.cpp" target="_blank" rel="noopener">Gitee</a>或<a href="https://github.com/VealM/UVaOJ/blob/master/Chapter5/textbook/5-2-3.cpp" target="_blank" rel="noopener">Github</a></p>
<a id="more"></a>
<h2 id="存储"><a href="#存储" class="headerlink" title="存储"></a>存储</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bign</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> len, s[maxn];</span><br><span class="line">    <span class="built_in">string</span> sign;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用整数数组对大整数进行<strong>逆序逐位</strong>存储(从下标0开始)，len记录长度，sign记录符号位(正数为空，负数为”-“)</p>
<h2 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h2><p>默认构造函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bign()&#123;<span class="built_in">memset</span>(s, <span class="number">0</span>, <span class="keyword">sizeof</span>(s)); len = <span class="number">1</span>;&#125;</span><br></pre></td></tr></table></figure>
<p>使用赋值运算定义有参构造函数</p>
<h3 id="重载赋值运算符"><a href="#重载赋值运算符" class="headerlink" title="重载赋值运算符"></a>重载赋值运算符</h3><p>声明</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> = (<span class="keyword">const</span> <span class="built_in">string</span>&amp; num);</span><br><span class="line">bign <span class="keyword">operator</span> = (<span class="keyword">const</span> <span class="keyword">int</span>&amp; num);</span><br></pre></td></tr></table></figure>

<p>注：const string&amp; 代表字符串的常量引用，也即无法通过该引用修改字符串的值（但不代表该字符串是常量字符串）。<br>另有一种说法，常量引用相比于常量效率要高，因此传参尽量使用常量引用（然而，我觉得真正理由如上条+引用不用拷贝因而效率高）。</p>
<p>思路：</p>
<ul>
<li>1 字符串赋值<br>逐字符读取string num，存储至数组；</li>
<li>2 整数初赋值<br>将int num通过ostringstream最终读到string num，调用字符串赋值</li>
</ul>
<p>实现</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">bign bign::<span class="keyword">operator</span> = (<span class="keyword">const</span> <span class="built_in">string</span>&amp; num)&#123;</span><br><span class="line">        len = num.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            s[i] = num[len - <span class="number">1</span> -i] - <span class="string">'0'</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>; <span class="comment">// 返回对象</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">bign bign::<span class="keyword">operator</span> = (<span class="keyword">const</span> <span class="keyword">int</span>&amp; num)&#123;</span><br><span class="line">        <span class="built_in">ostringstream</span> numstr;</span><br><span class="line">        numstr &lt;&lt; num;</span><br><span class="line">        <span class="built_in">string</span> bnum = numstr.str();</span><br><span class="line">        *<span class="keyword">this</span> = bnum;</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="有参构造函数"><a href="#有参构造函数" class="headerlink" title="有参构造函数"></a>有参构造函数</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">bign(<span class="keyword">const</span> <span class="built_in">string</span>&amp; num)&#123;*<span class="keyword">this</span> = num;&#125;</span><br><span class="line">bign(<span class="keyword">const</span> <span class="keyword">int</span>&amp; num)&#123;*<span class="keyword">this</span> = num;&#125;</span><br></pre></td></tr></table></figure>

<p>使用示例</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">string</span> s = <span class="string">"123456789"</span>;</span><br><span class="line"><span class="function">bign <span class="title">num1</span><span class="params">(s)</span></span>;</span><br><span class="line"><span class="keyword">int</span> n2 = <span class="number">1234567</span>;</span><br><span class="line"><span class="function">bign <span class="title">num2</span><span class="params">(n2)</span></span>;</span><br></pre></td></tr></table></figure>

<p><strong>疑问</strong>：是否应考虑如下初始化方式</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">bign <span class="title">num</span><span class="params">(<span class="string">"123456"</span>)</span></span>;</span><br></pre></td></tr></table></figure>

<p>这里，”123456”的类型是const char[6]，我考虑过以下构造函数，但显然强制转换也不能这么搞 const char* s = “123456”;所以这个问题暂时不知道怎么解决。后面输入流间接解决了这个问题。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bign(<span class="keyword">const</span> <span class="keyword">char</span>* s);</span><br></pre></td></tr></table></figure>

<h2 id="输入输出流"><a href="#输入输出流" class="headerlink" title="输入输出流"></a>输入输出流</h2><p>输入输出流为非成员函数接口。</p>
<h3 id="输入流"><a href="#输入流" class="headerlink" title="输入流"></a>输入流</h3><p>读入bign对象</p>
<p>思路<br>从流对象in中读取string，并用string对bign x赋值，返回流对象in<br><em>此处为 bign&amp; x是因为输入流改变了x，因此不能为const bign&amp; x</em><br>实现</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">istream&amp; <span class="keyword">operator</span> &gt;&gt; (istream &amp;in, bign&amp; x)</span><br><span class="line">&#123;</span><br><span class="line">        <span class="built_in">string</span> s;</span><br><span class="line">        in &gt;&gt; s;</span><br><span class="line">        x = s;</span><br><span class="line">        <span class="keyword">return</span> in;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="输出流"><a href="#输出流" class="headerlink" title="输出流"></a>输出流</h3><h4 id="str方法"><a href="#str方法" class="headerlink" title="str方法"></a>str方法</h4><p>我们通过先定义bign的str()方法（成员函数），再据此给出输出流的实现</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="built_in">string</span> <span class="title">bign::str</span><span class="params">()</span> <span class="keyword">const</span></span>&#123;</span><br><span class="line">        <span class="built_in">string</span> numstr;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            numstr = <span class="keyword">char</span>(s[i] + <span class="string">'0'</span>) + numstr;</span><br><span class="line">        &#125;</span><br><span class="line">        numstr = sign + numstr; <span class="comment">//符号位输出</span></span><br><span class="line">        <span class="keyword">if</span> (numstr.empty()) numstr = <span class="string">"0"</span>;</span><br><span class="line">        <span class="keyword">return</span> numstr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ostream&amp; <span class="keyword">operator</span> &lt;&lt; (ostream &amp;out, <span class="keyword">const</span> bign&amp; x)</span><br><span class="line">&#123;</span><br><span class="line">        out &lt;&lt; x.str();</span><br><span class="line">        <span class="keyword">return</span> out;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h2><p>先定义比较运算符，因为减法、除法都能用得到</p>
<h3 id="比较关系符"><a href="#比较关系符" class="headerlink" title="比较关系符"></a>比较关系符</h3><h4 id="lt"><a href="#lt" class="headerlink" title="&lt;"></a>&lt;</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">bool</span> bign::<span class="keyword">operator</span>&lt; (<span class="keyword">const</span> bign &amp;b) <span class="keyword">const</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (len != b.len) <span class="keyword">return</span> len &lt; b.len;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = len - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (s[i] != b.s[i]) <span class="keyword">return</span> s[i] &lt; b.s[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后向大家展示如何用&lt;定义完所有比较符（这大约就是数学的魔力吧）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">bool</span> bign:: <span class="keyword">operator</span> &gt; (<span class="keyword">const</span> bign &amp;b) <span class="keyword">const</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> b &lt; *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">bool</span> bign:: <span class="keyword">operator</span>&lt;= (<span class="keyword">const</span> bign &amp;b) <span class="keyword">const</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> !(b &lt; *<span class="keyword">this</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">bool</span> bign:: <span class="keyword">operator</span>&gt;= (<span class="keyword">const</span> bign &amp;b) <span class="keyword">const</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> !(*<span class="keyword">this</span> &lt; b);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">bool</span> bign:: <span class="keyword">operator</span>!= (<span class="keyword">const</span> bign &amp;b) <span class="keyword">const</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (*<span class="keyword">this</span> &lt; b) || (b &lt; *<span class="keyword">this</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">bool</span> bign:: <span class="keyword">operator</span>== (<span class="keyword">const</span> bign &amp;b) <span class="keyword">const</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> !(*<span class="keyword">this</span> &lt; b) &amp;&amp; !(b &lt; *<span class="keyword">this</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="算术关系符"><a href="#算术关系符" class="headerlink" title="算术关系符"></a>算术关系符</h3><h4 id=""><a href="#" class="headerlink" title="+"></a>+</h4><p>模拟手算，需要注意如果相加至最高位仍有进位，需要记录</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> + (<span class="keyword">const</span> bign&amp; a , <span class="keyword">const</span> bign&amp; b)</span><br><span class="line">&#123;</span><br><span class="line">    bign c = <span class="number">0</span>;</span><br><span class="line">    c.len = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>, g = <span class="number">0</span>; i &lt; max(a.len, b.len) || g; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> x = g;</span><br><span class="line">        <span class="keyword">if</span> (i &lt; a.len) x += a.s[i];</span><br><span class="line">        <span class="keyword">if</span> (i &lt; b.len) x += b.s[i];</span><br><span class="line">        </span><br><span class="line">        c.s[c.len++] = x % <span class="number">10</span>;</span><br><span class="line">        g = x / <span class="number">10</span>;        </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> c;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>根据cpp primer中的设计准则而言，将+=定义为bign类的成员函数，在此基础上将+定义为非成员函数更合适…但我感觉+=真的蛮容易写混的…干脆还是从+写起，也方便其他人查看思路</p>
<h4 id="-1"><a href="#-1" class="headerlink" title="+="></a>+=</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">bign <span class="keyword">operator</span> += (bign&amp; a, <span class="keyword">const</span> bign&amp; b)</span><br><span class="line">&#123;</span><br><span class="line">    a = a + b;</span><br><span class="line">    <span class="keyword">return</span> a;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="-2"><a href="#-2" class="headerlink" title="*"></a>*</h4><p>其实乘法比减法好写<br>思路还是逐位模拟手算，这里比较妙的一点是逐位乘积相加时，我直接在存储上操作完成了<strong>乘以10的幂次</strong>效果</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> * (<span class="keyword">const</span> bign&amp; a, <span class="keyword">const</span> bign&amp; b)&#123;</span><br><span class="line">    <span class="keyword">if</span> (a == <span class="number">0</span> || b == <span class="number">0</span>) <span class="keyword">return</span> bign(<span class="number">0</span>);</span><br><span class="line">    <span class="keyword">if</span> (a == <span class="number">1</span>) <span class="keyword">return</span> b;</span><br><span class="line">    <span class="keyword">if</span> (b == <span class="number">1</span>) <span class="keyword">return</span> a;</span><br><span class="line">    bign res = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> j, i, g;</span><br><span class="line">    <span class="keyword">for</span> ( j = <span class="number">0</span>; j &lt; b.len; j++)</span><br><span class="line">    &#123;</span><br><span class="line">        bign c = <span class="number">0</span>; c.len = j; <span class="comment">// 乘子 b[j]</span></span><br><span class="line">        <span class="keyword">for</span> ( i = <span class="number">0</span>, g = <span class="number">0</span>; i &lt; a.len || g; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> multi = g + b.s[j]*a.s[i];</span><br><span class="line">            g = multi / <span class="number">10</span>;</span><br><span class="line">            c.s[c.len++] = multi % <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        res += c;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>据说乘法可以用递归算法，效率更高，后期有空补上（</p>
<h4 id="-3"><a href="#-3" class="headerlink" title="*="></a>*=</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> *= (bign&amp; a, <span class="keyword">const</span> bign&amp; b)&#123;</span><br><span class="line">    a = a * b;</span><br><span class="line">    <span class="keyword">return</span> a;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="-4"><a href="#-4" class="headerlink" title="-"></a>-</h4><p>好像现在想想，可能先实现一个大数减小数的方法，然后通过比较进行不同的调用会简洁一些…</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> - (<span class="keyword">const</span> bign&amp; a ,<span class="keyword">const</span> bign&amp; b)</span><br><span class="line">&#123;</span><br><span class="line">    bign c = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (a == b) <span class="keyword">return</span> c;</span><br><span class="line">    c.len = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (a &gt; b)&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>,g = <span class="number">0</span>; i &lt; max(a.len, b.len); i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> x = a.s[i] - b.s[i] - g;</span><br><span class="line">            <span class="keyword">if</span> (x &lt; <span class="number">0</span>)&#123;</span><br><span class="line">                x += <span class="number">10</span>; g = <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> g = <span class="number">0</span>;</span><br><span class="line">            c.s[c.len++] = x; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        c.sign = <span class="string">"-"</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>, g = <span class="number">0</span>; i &lt; max(a.len, b.len); i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> x = b.s[i] - a.s[i] - g;</span><br><span class="line">            <span class="keyword">if</span> (x &lt; <span class="number">0</span>)&#123;</span><br><span class="line">                x += <span class="number">10</span>; g = <span class="number">1</span>;</span><br><span class="line">            &#125; </span><br><span class="line">            <span class="keyword">else</span> g = <span class="number">0</span>;</span><br><span class="line">            c.s[c.len++] = x;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//清除前导0</span></span><br><span class="line">    <span class="keyword">int</span> j;</span><br><span class="line">    <span class="keyword">for</span> (j = c.len <span class="number">-1</span>; c.s[j]==<span class="number">0</span>; j--)&#123;&#125;</span><br><span class="line">    c.len = j + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> c;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="-5"><a href="#-5" class="headerlink" title="-="></a>-=</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> -= (bign&amp; a, <span class="keyword">const</span> bign&amp; b)&#123;</span><br><span class="line">    a = a - b;</span><br><span class="line">    <span class="keyword">return</span> a;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="-6"><a href="#-6" class="headerlink" title="/"></a>/</h4><p>对于除法写的不满意，其中涉及到一个频繁的运算(*=10)，其实就是一个append(0)操作，但是数组的话是O(N)复杂度，所以我就偷懒用大整数乘法了，感觉<strong>10的幂次乘是不是作为乘法的特殊样例进行快速处理</strong>比较好？<br>这里也做了个前导零清除的操作，其实可以在第一次遇到商的非零位记录，算出来res.len的。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> / (<span class="keyword">const</span> bign&amp; a, <span class="keyword">const</span> bign&amp; b)&#123;</span><br><span class="line">    <span class="keyword">if</span> (b == <span class="number">0</span>) <span class="keyword">throw</span> runtime_error(<span class="string">"could not divided by zero!"</span>);</span><br><span class="line">    <span class="keyword">if</span> (a &lt; b) <span class="keyword">return</span> bign(<span class="number">0</span>);</span><br><span class="line">    <span class="keyword">if</span> (a == b) <span class="keyword">return</span> bign(<span class="number">1</span>);</span><br><span class="line">    <span class="function">bign <span class="title">res</span><span class="params">(<span class="number">0</span>)</span></span>; res.len = a.len;</span><br><span class="line">    <span class="function">bign <span class="title">c</span><span class="params">(<span class="number">0</span>)</span></span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = a.len - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--)</span><br><span class="line">    &#123;</span><br><span class="line">        c += a.s[i];</span><br><span class="line">        <span class="keyword">if</span> (c == b)</span><br><span class="line">        &#123;</span><br><span class="line">            c = <span class="number">0</span>; res.s[i] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (c &lt; b)</span><br><span class="line">        &#123;</span><br><span class="line">            c *= <span class="number">10</span>; res.s[i] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> k = <span class="number">0</span>;</span><br><span class="line">            <span class="function">bign <span class="title">tot</span><span class="params">(<span class="number">0</span>)</span></span>;</span><br><span class="line">            <span class="keyword">while</span> (tot &lt;= c)</span><br><span class="line">            &#123;</span><br><span class="line">                tot += b;</span><br><span class="line">                k++;</span><br><span class="line">            &#125;</span><br><span class="line">            tot -= b;</span><br><span class="line">            c -= tot;</span><br><span class="line">            c *= <span class="number">10</span>;</span><br><span class="line">            res.s[i] = k - <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 清除前导0</span></span><br><span class="line">    <span class="keyword">int</span> j;</span><br><span class="line">    <span class="keyword">for</span> (j = res.len - <span class="number">1</span>; res.s[j]==<span class="number">0</span>; j--);</span><br><span class="line">    res.len = j + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>说到这里不得不提某个隐藏bug，它就藏在while循环里…之前我是这么写的</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> (c &gt;= <span class="number">0</span>)</span><br><span class="line">&#123;</span><br><span class="line">        c -= b;</span><br><span class="line">        k++;</span><br><span class="line">&#125;</span><br><span class="line">c += b;</span><br><span class="line">res.s[i] = k - <span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<p>也就是说，当 c&gt;= 0 不满足时跳出while循环，但此时c为负数，负数不能与0进行比较（因为比较运算符仅限非负整数间比较）！</p>
<p>这个bug最终靠gdb调试查出来，据说说好的代码都不靠gdb抓bug…一笔写成…只能吐槽自己还是太菜啦</p>
<p>另一个问题是<strong>非法输入处理</strong>(除数为0)，之前看c++ primer有try和throw，我也用了下…但感觉这个处理不是很优雅…因为我尝试非法输入后，尽管显示了提示信息，但exe程序好像还是崩溃了…回忆了下，似乎cpp中如果输入除数为0，直接返回inf。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (b == <span class="number">0</span>) <span class="keyword">throw</span> runtime_error(<span class="string">"could not divided by zero!"</span>);</span><br></pre></td></tr></table></figure>

<h4 id="-7"><a href="#-7" class="headerlink" title="/="></a>/=</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">bign <span class="keyword">operator</span> /= (bign&amp; a, <span class="keyword">const</span> bign&amp; b)&#123;</span><br><span class="line">    a = a / b;</span><br><span class="line">    <span class="keyword">return</span> a;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当时还写了个除法操作，也是模拟手算，但异常麻烦，只是不用处理前导0问题了，我注释掉了。</p>
<h2 id="测试单元"><a href="#测试单元" class="headerlink" title="测试单元"></a>测试单元</h2><p>测试主函数还是非常漂亮的</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    bign num1, num2;</span><br><span class="line">    <span class="built_in">cin</span> &gt;&gt; num1 &gt;&gt; num2;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; num1 + num2 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; num1 - num2 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; num1 * num2 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; num1 / num2 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>后期还可以把类定义放在bign.h里，然后函数实现放在bign.cpp，用到bign类的文件名为main.cpp，不过最终编译的时候要自己写个makefile来编译链接，有空再补上~（毕竟是win下脱离了IDE写代码我也好苦…）</p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://vealm.gitee.io/2020/08/14/bign/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/cpp%E7%BB%BC%E5%90%88/" rel="tag">cpp综合</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/08/15/mlp/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            碎语
          
        </div>
      </a>
    
    
      <a href="/2020/08/06/UVaOJ3/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">入门经典第三章之字符串（下）</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "854ziDXtPQxK8dJf2vsosmzN-gzGzoHsz",
    app_key: "kV9lcyv3dVA2I6pqA4yd23p6",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2020
        <i class="ri-heart-fill heart_icon"></i> Veal
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        Powered by <a href="https://hexo.io" target="_blank">Hexo</a>
        <span class="division">|</span>
        Theme - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278599897&amp;web_id=1278599897'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Veal&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/Me">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    
    <!--加入音乐播放器-->

    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->


    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css">
        <script src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js"></script>
        
    


<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>